import time
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testjob:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/1_get_job_list.yaml'))
    def test_get_job_list(self, jobinfo):
        # 获取job列表接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = base_url + jobinfo['request']['url'].replace('{namespace}', 'ingress-nginx')
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/1_get_job_list_exception.yaml'))
    def test_get_job_list_exception(self, jobinfo):
        # 获取job列表接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = base_url + jobinfo['request']['url'].replace('{namespace}', 'ingress-nginx')
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/2_create_job.yaml'))
    def test_create_job(self, jobinfo):
        # 创建job接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = base_url + jobinfo['request']['url'].replace('{namespace}', 'default')
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        jobname = req.json()['metadata']['name']
        globals()['jobname'] = jobname
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/2_create_job_exception.yaml'))
    def test_create_job_exception(self, jobinfo):
        # 创建job接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = base_url + jobinfo['request']['url'].replace('{namespace}', 'default')
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/3_get_specific_job.yaml'))
    def test_get_specific_job(self, jobinfo):
        # 获取指定job接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/job/3_get_specific_job_exception.yaml'))
    def test_get_specific_job_exception(self, jobinfo):
        # 获取指定job接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/4_update_job.yaml'))
    def test_update_job(self, jobinfo):
        # 更新指定job接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        data = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/4_update_job_exception.yaml'))
    def test_update_job_exception(self, jobinfo):
        # 更新指定job接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        data = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/5_update_section_job.yaml'))
    def test_update_section_job(self, jobinfo):
        # 部分更新指定job的接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        header = jobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/job/5_update_section_job_exception.yaml'))
    def test_update_section_job_exception(self, jobinfo):
        # 部分更新指定job的接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        data = jobinfo['request']['data']
        header = jobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/6_delete_specific_job.yaml'))
    def test_delete_specific_job(self, jobinfo):
        # 删除job接口-正常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('jobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/job/6_delete_specific_job_exception.yaml'))
    def test_delete_specific_job_exception(self, jobinfo):
        # 删除job接口-异常
        base_url = datas.baseUrl + Testjob.url_unify
        url = (base_url + jobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{jobname}', globals()['jobname']))
        method = jobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == jobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.job
    @pytest.mark.parametrize('podinfo', YamlUtil().read_testcase_yaml('v25.06/resourcemanage/job/7_get_pod_list.yaml'))
    def test_delete_pod_createby_job(self, podinfo):
        # 获取job任务生成的pod，并删除
        time.sleep(3)
        base_url = datas.baseUrl + Testjob.url_unify
        url = base_url + podinfo['request']['url'].replace('{namespace}', 'default')
        method = podinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        podlist = req.json()['items']
        for i in podlist:
            if 'librespeed-job' in i['metadata']['name']:
                podname = i['metadata']['name']
                del_url = url + '/' + podname.replace('{podname}', podname)
                req = RequestUtil().send_request('delete', del_url)
        assert req.status_code == podinfo['validate'][0]['equals']['status_code']
